package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
//部门管理控制层
@RestController
@RequestMapping("/depts")
@SuppressWarnings("all")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除部门id：{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门：{}",dept);
        deptService.update(dept);
        return Result.success();

    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        log.info("查询部门id：{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);

    }

    @GetMapping
    public Result page(
            //注解的作用：从前端请求？的路径取出参数赋值给变量
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer PageSize){

        PageResult<Dept> pageResult = deptService.page(name, status, page, PageSize);
        log.info("分页查询，参数：name:{},status:{},page:{},pageSize:{}",name,status,page,PageSize);
        log.info("-----------------------++++++部门列表方法+++++--------------------------");
        return Result.success(pageResult);
    }
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("添加部门：{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping("/list")
    public Result listAll(){
       List<Dept> list=  deptService.listAll();
       return Result.success(list);
    }
}
